<?php
/**
 * 用户token
 * User: minyifei
 * Date: 15/7/8
 * Time: 下午4:27
 * table:
 CREATE TABLE `user_token` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL COMMENT '用户编号',
`token` varchar(150) NOT NULL COMMENT '用户token',
`create_time` datetime NOT NULL COMMENT '创建时间',
PRIMARY KEY (`id`),
UNIQUE KEY `user_id` (`user_id`),
UNIQUE KEY `token` (`token`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户token表'
 */

namespace MyfApi\Model;

class UserToken extends \Myf\Mvc\Model {

    /**
     * 获取token
     * @param $userId
     * @return string
     */
    public static function getToken($userId){
        $now = getCurrentTime();
        //创建token
        $token = createToken($userId);
        $ut = new UserToken();
        $data = $ut->findFirst("user_id",$userId);
        if($data){
            $ud = array(
                "token"=>$token,
                "update_time"=>$now,
            );
            $ut->update($ud,"id=:id",array("id"=>$data["id"]));
        }else{
            $td = array(
                "user_id"=>$userId,
                "token"=>$token,
                "create_time"=>$now,
                "update_time"=>$now,
            );
            $ut->insert($td);
        }
        return $token;
    }

}